﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.Base
{
    public partial class frmLoading : DevComponents.DotNetBar.Office2007Form
    {
        private SqlConnection _sqlConnection;
        public string nameFile = "D:\\DataBaseConfig.txt";

        public frmLoading()
        {
            InitializeComponent();
            timer1.Enabled = true;
            timer1.Start();
            var t1 = new Thread(LoadConfig);
            t1.Start();

            //t1.Abort();
        }

        public static string Servername { get; set; }
        public static string UserName { get; set; }
        public static string Pass { get; set; }

        public void LoadConfig()
        {
            string c = "";

            int i = 0;
            if (File.Exists(nameFile))
            {
                StreamReader doc = File.OpenText(nameFile);

                while ((c = doc.ReadLine()) != null)
                {
                    i = i + 1;
                    if (i == 1)
                        Servername = c;
                    if (i == 2)
                        UserName = c;
                    if (i == 3)
                        Pass = c;
                }
                doc.Close();
            }
            else
            {
                Servername = "533.0.0.1";
                UserName = "sa";
                Pass = "1";
                var t = new FileInfo(nameFile);
                StreamWriter g = t.CreateText();

                g.WriteLine(Servername);
                g.WriteLine(UserName);
                g.WriteLine(Pass);
                g.Close();
            }
            string connectionString = string.Format("Data Source={0};Initial Catalog=TuyenSinh;User ID={1};Password={2}", Servername, UserName, Pass);
            _sqlConnection = new SqlConnection(connectionString);
            SqlCommand lenh = _sqlConnection.CreateCommand();
            lenh.CommandText = "select * from UserLogin";
            try
            {
                _sqlConnection.Open();
                lenh.ExecuteNonQuery();
                _sqlConnection.Close();
                timer1.Stop();
                lbLoading.BeginInvoke((Action)(() => { lbLoading.Text = Resources.frmLoading_LoadConfig_ConnectSuccess; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                if (ProgressStatus.ProgressBar != null)
                    ProgressStatus.ProgressBar.BeginInvoke((Action) (() => { ProgressStatus.Value = 100; }));
                lblCurrentValue.BeginInvoke((Action) (() => { lblCurrentValue.Text = "100 %"; }));
                lblCurrentValue.BeginInvoke((Action) (() => { lblCurrentValue.Visible = false; }));
                groupLoading.BeginInvoke((Action) (() => { groupLoading.Visible = false; }));
                groupLogin.BeginInvoke((Action) (() => { groupLogin.Visible = true; }));
                groupChangeConfig.BeginInvoke((Action)(() => { groupChangeConfig.Visible = false; }));
                txtUserId.BeginInvoke((Action) (() => txtUserId.Focus()));
            }
            catch
            {
                _sqlConnection.Close();
                lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_LoadConfig_ErrorConnection; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                lblCurrentValue.BeginInvoke((Action) (() => { lblCurrentValue.Visible = true; }));
                groupLoading.BeginInvoke((Action) (() => { groupLoading.Visible = false; }));
                groupLogin.BeginInvoke((Action) (() => { groupLogin.Visible = false; }));
                groupChangeConfig.BeginInvoke((Action)(() => { groupChangeConfig.Visible = true; }));
                txtServerName.BeginInvoke((Action) (() => txtServerName.Focus()));
                timer1.Stop();
            }
        }

        private void Timer1Tick(object sender, EventArgs e)
        {
            ProgressStatus.Increment(1);
            lblCurrentValue.BeginInvoke((Action) (() => { lblCurrentValue.Text = ProgressStatus.Value.ToString() + " %"; }));
            if (ProgressStatus.Value == 100)
                timer1.Stop();
        }

        private void ReadOnyTextBox(bool isReadOnly)
        {
            txtServerName.ReadOnly = isReadOnly;
            txtUserName.ReadOnly = isReadOnly;
            txtPassword.ReadOnly = isReadOnly;
        }

        private void FrmLoadingLoad(object sender, EventArgs e)
        {
            groupLoading.BeginInvoke((Action) (() => { groupLoading.Visible = true; }));
            groupChangeConfig.BeginInvoke((Action)(() => { groupChangeConfig.Visible = false; }));
            groupLogin.BeginInvoke((Action) (() => { groupLogin.Visible = false; }));
            ReadOnyTextBox(false);
            lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_FrmLoadingLoad_Loading; }));
            lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Blue; }));
            if (ProgressStatus.ProgressBar != null)
                ProgressStatus.ProgressBar.Invoke((Action) (() => { ProgressStatus.Value = 0; }));
            lblCurrentValue.BeginInvoke((Action) (() => { lblCurrentValue.Text = "0 %"; }));
            lblCurrentValue.BeginInvoke((Action) (() => { lblCurrentValue.Visible = true; }));
            BtnCancelConfigClick(sender, e);
            timer1.Enabled = true;
            timer1.Start();
        }

        private void BtnCancelConfigClick(object sender, EventArgs e)
        {
            txtPassword.Clear();
            txtServerName.Clear();
            txtUserName.Clear();
        }

        private void BtnSaveClick(object sender, EventArgs e)
        {
            if (CheckValidServer())
            {
                DialogResult hop = MessageBox.Show(Resources.frmLoading_BtnSaveClick_ConfirmSave, Resources.frmLoading_LoadConfig_Header, MessageBoxButtons.YesNo);
                if (hop == DialogResult.Yes)
                {
                    if (File.Exists(nameFile))
                    {
                        try
                        {
                            var g = new StreamWriter(nameFile, false);
                            g.WriteLine(txtServerName.Text.Trim());
                            g.WriteLine(txtUserName.Text.Trim());
                            g.WriteLine(txtPassword.Text.Trim());
                            g.Close();
                            FrmLoadingLoad(sender, e);
                            var t1 = new Thread(LoadConfig);
                            t1.Start();
                        }
                        catch
                        {
                            MessageBox.Show(Resources.frmLoading_BtnSaveClick_SaveFail, Resources.frmLoading_LoadConfig_Header);
                        }
                    }
                }
            }
        }

        private bool CheckValidServer()
        {
            if (string.IsNullOrEmpty(txtServerName.Text.Trim()))
            {
                lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_CheckValidServer_SeverNameNull; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                txtServerName.BeginInvoke((Action) (() => txtServerName.Focus()));
                return false;
            }
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_CheckValidLogin_NameNull; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                txtUserName.BeginInvoke((Action) (() => txtUserName.Focus()));
                return false;
            }
            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_CheckValidLogin_PasswordNull; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                txtPassword.BeginInvoke((Action) (() => txtPassword.Focus()));
                return false;
            }
            return true;
        }

        private void BtnExitClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void BtnLoginClick(object sender, EventArgs e)
        {
            try
            {
                if (CheckValidLogin())
                {
                    var userLogin = new UserLogin();
                    userLogin.UserName = txtUserId.Text.Trim();
                    userLogin.Password = txtPassUser.Text.Trim();
                    DataTable table = userLogin.GetUserLoginByUserNameAndPass(userLogin);
                    if (table.Rows.Count > 0)
                    {
                        ShowFormMain();
                    }
                    else
                    {
                        lbLoading.BeginInvoke((Action)(() => { lbLoading.Text = "Sai tên đăng nhập hoặc password"; }));
                        lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void ShowFormMain()

        {
            var frm = new frmMain();
            frm.Visible = false;
            Hide();
            frm.ShowDialog();
        }

        private bool CheckValidLogin()
        {
            if (string.IsNullOrEmpty(txtUserId.Text.Trim()))
            {
                lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_CheckValidLogin_NameNull; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                txtUserId.BeginInvoke((Action) (() => txtUserId.Focus()));
                return false;
            }
            if (string.IsNullOrEmpty(txtPassUser.Text.Trim()))
            {
                lbLoading.BeginInvoke((Action) (() => { lbLoading.Text = Resources.frmLoading_CheckValidLogin_PasswordNull; }));
                lbLoading.BeginInvoke((Action) (() => { lbLoading.ForeColor = Color.Red; }));
                txtPassUser.BeginInvoke((Action) (() => txtPassUser.Focus()));
                return false;
            }
            return true;
        }

     

       
    }
}